home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-06-25 | 15.8 KB | 377 lines | [TEXT/CCL ] |
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; File: Rule-Demo.lisp
- ; Author: Dan Suthers
- ; Created: 21-Mar-89 13:21:00
- ; Modified: 24-Jun-90 23:14:36 (Dan Suthers)
- ; Language: Common Lisp
- ; Package: USER
- ;
- ; Description: Some examples to run rules on. I've thrown together two
- ; examples which I used for testing: reasoning about values
- ; in an electric circuit, and reasoning about relationships.
- ; This demos mainly the backchainer.
- ;
- ; Note that some of these examples show problems with the
- ; backchainer, in particular redundant reasoning. I included
- ; these to help you evaluate the limitations of this package.
- ;
- ; You should evalute the database and rule defining sections
- ; as a group, but evaluate the individual rule invocations
- ; and graphing expressions one at a time to see what
- ; happens. Use the graphical interface (clicking on nodes)
- ; to examine the support tree, and the SM Browser to look
- ; at the rules.
- ;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- (in-package "USER")
-
- (defun DEMO-GRAPH-SUPPORT-TREE (root-node)
- (ccl:oneof grapher:*graph-window*
- :window-title (rule::label-string (rule::trj-node-claim root-node) 10 40)
- :window-size (ccl:make-point (- *screen-width* 4) 280)
- :window-position (ccl:make-point 2 ccl:*menubar-bottom*)
- :graph-view
- (rule::trj-node->graph-view root-node
- :vertical-tree
- :as-found
- 6
- nil)))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;; CIRCUITS: Forward and Back Chaining
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;; Representation of a circuit in a DNET database. Evaluate all of this
- ;;; section as a group. Note here I use lower level DNET building functions
- ;;; instead of MAKE-DATA-BASE and ADD-DATUM.
-
- ;;; dnet:make-dnet is what rule:make-data-base calls (virtually identical).
-
- (dnet:make-dnet ':CIRCUIT-1
- :indexpr-hook 'NIL
- :delexpr-hook 'NIL
- :info 'NIL)
-
- (dnet:indexpr '(PORTS (BATTERY B1) (PORT PB1-1) (PORT PB1-2)) ':CIRCUIT-1 '(#S(RULE:JUSTIFICATION RULE::WARRANT :DATUM RULE::GROUNDS NIL TIME 2807247882)))
- (dnet:indexpr '(PORTS (RESISTOR R1) (PORT PR1-1) (PORT PR1-2)) ':CIRCUIT-1 '(#S(RULE:JUSTIFICATION RULE::WARRANT :DATUM RULE::GROUNDS NIL TIME 2807247882)))
- (dnet:indexpr '(PORTS (LIGHT L1) (PORT PL1-1) (PORT PL1-2)) ':CIRCUIT-1 '(#S(RULE:JUSTIFICATION RULE::WARRANT :DATUM RULE::GROUNDS NIL TIME 2807247882)))
- (dnet:indexpr '(PORTS (LIGHT L2) (PORT PL2-1) (PORT PL2-2)) ':CIRCUIT-1 '(#S(RULE:JUSTIFICATION RULE::WARRANT :DATUM RULE::GROUNDS NIL TIME 2807247882)))
- (dnet:indexpr '(VOLTAGE (BATTERY B1) (VOLTS 6)) ':CIRCUIT-1 '(#S(RULE:JUSTIFICATION RULE::WARRANT :DATUM RULE::GROUNDS NIL TIME 2807247882)))
- (dnet:indexpr '(RESISTANCE (RESISTOR R1) (OHMS 10)) ':CIRCUIT-1 '(#S(RULE:JUSTIFICATION RULE::WARRANT :DATUM RULE::GROUNDS NIL TIME 2807247882)))
- (dnet:indexpr '(RESISTANCE (LIGHT L1) (OHMS 2)) ':CIRCUIT-1 '(#S(RULE:JUSTIFICATION RULE::WARRANT :DATUM RULE::GROUNDS NIL TIME 2807247882)))
- (dnet:indexpr '(RESISTANCE (LIGHT L2) (OHMS 2)) ':CIRCUIT-1 '(#S(RULE:JUSTIFICATION RULE::WARRANT :DATUM RULE::GROUNDS NIL TIME 2807247882)))
- (dnet:indexpr '(WIRE (PORT PB1-1) (PORT PL1-1)) ':CIRCUIT-1 '(#S(RULE:JUSTIFICATION RULE::WARRANT :DATUM RULE::GROUNDS NIL TIME 2807247882)))
- (dnet:indexpr '(WIRE (PORT PB1-2) (PORT PL1-2)) ':CIRCUIT-1 '(#S(RULE:JUSTIFICATION RULE::WARRANT :DATUM RULE::GROUNDS NIL TIME 2807247882)))
- (dnet:indexpr '(WIRE (PORT PL1-1) (PORT PR1-1)) ':CIRCUIT-1 '(#S(RULE:JUSTIFICATION RULE::WARRANT :DATUM RULE::GROUNDS NIL TIME 2807247882)))
- (dnet:indexpr '(WIRE (PORT PL1-2) (PORT PL2-2)) ':CIRCUIT-1 '(#S(RULE:JUSTIFICATION RULE::WARRANT :DATUM RULE::GROUNDS NIL TIME 2807247882)))
- (dnet:indexpr '(WIRE (PORT PR1-2) (PORT PL2-1)) ':CIRCUIT-1 '(#S(RULE:JUSTIFICATION RULE::WARRANT :DATUM RULE::GROUNDS NIL TIME 2807247882)))
- (dnet:indexpr '(EFFICIENCY (LIGHT L2) (RATIO 2/3)) ':CIRCUIT-1 '(#S(RULE:JUSTIFICATION RULE::WARRANT :DATUM RULE::GROUNDS NIL TIME 2807249648)))
- (dnet:indexpr '(EFFICIENCY (LIGHT L1) (RATIO 2/3)) ':CIRCUIT-1 '(#S(RULE:JUSTIFICATION RULE::WARRANT :DATUM RULE::GROUNDS NIL TIME 2807249626)))
-
- ;;; Get a bunch of variables out of the way ...
- (rule:DEFVARIABLES "A" "ATTRIBUTE" "C1" "C2" "CTYPE1" "CTYPE2"
- "D" "DEV" "E" "INSTANCE1" "INSTANCE2" "L"
- "LUM" "NEG-V" "OBJECT-TYPE" "P1" "P2" "P3"
- "P4" "R" "R1" "R2" "SCALE" "V" "V1" "V2" "VALUE-TYPE")
-
- (rule:rule BRIGHTNESS
- :ANTECEDENT
- (:seq (ports (light ?:l) (port ?:p1) (port ?:p2))
- (efficiency (light ?:l) (ratio ?:e))
- (current (port ?:p1) (port ?:p2) (amps ?:a))
- (:bind ?:lum (* ?:e ?:a)))
- :CONSEQUENT (brightness (light ?:l) (lumens ?:lum))
- :DIRECTIONS :backward
- :INTERNED-IN (:network-rules)
- :INFO ((:domain . :networks))
- :COMMENTS "Says how to find the brightness of a light as a function of current.")
-
- (rule:rule OHMS-LAW-FOR-CURRENT
- :ANTECEDENT
- (:seq (resistance (port ?:p1) (port ?:p2) (ohms ?:r))
- (voltage (port ?:p1) (port ?:p2) (volts ?:v))
- (:bind ?:a (/ ?:v ?:r)))
- :CONSEQUENT (current (port ?:p1) (port ?:p2) (amps ?:a))
- :DIRECTIONS :backward
- :INTERNED-IN (:network-rules)
- :INFO ((:domain . :networks))
- :COMMENTS "Given resistance and voltage across ports, provides current flow.")
-
- (rule:rule PROPORTION
- :ANTECEDENT
- (:seq (?:attribute (?:object-type ?:instance1) (?:value-type ?:v1))
- (?:attribute (?:object-type ?:instance2) (?:value-type ?:v2))
- (:bind ?:r (/ ?:v1 ?:v2)))
- :CONSEQUENT
- (proportion (?:attribute (?:object-type ?:instance1))
- (?:attribute (?:object-type ?:instance2))
- (ratio ?:r))
- :DIRECTIONS :backward
- :INTERNED-IN (:network-rules)
- :INFO ((:domain . :networks))
- :COMMENTS "Says how to find the proportion between two attributes.")
-
- (rule:rule RESISTANCE-ACROSS-DEVICE-PORTS
- :ANTECEDENT
- (:and (ports (?:dev ?:d) (port ?:p1) (port ?:p2))
- (resistance (?:dev ?:d) (?:scale ?:r)))
- :CONSEQUENT
- (:and (resistance (port ?:p1) (port ?:p2) (?:scale ?:r))
- (resistance (port ?:p2) (port ?:p1) (?:scale ?:r)))
- :DIRECTIONS :forward
- :INTERNED-IN (:network-rules)
- :INFO ((:domain . :networks))
- :COMMENTS "Assigns the resistance of a device to its ports.")
-
- (rule:rule SERIES-CURRENT
- :ANTECEDENT
- (:and
- (series-network (port ?:p1)
- (port ?:p2)
- (port ?:p3)
- (port ?:p4))
- (current (port ?:p1) (port ?:p4) (?:scale ?:a)))
- :CONSEQUENT (current (port ?:p3) (port ?:p4) (?:scale ?:a))
- :DIRECTIONS :backward
- :INTERNED-IN (:network-rules)
- :INFO nil
- :COMMENTS
- "Says that the current across an entire series network is
- also the current across subcomponents.")
-
- (rule:rule SERIES-NETWORK
- :ANTECEDENT
- (:and (ports (?:ctype1 ?:c1) (port ?:p1) (port ?:p2))
- (ports (?:ctype2 ?:c2) (port ?:p3) (port ?:p4))
- (wire (port ?:p2) (port ?:p3)))
- :CONSEQUENT
- (:and
- (series-network (port ?:p1)
- (port ?:p2)
- (port ?:p3)
- (port ?:p4))
- (series-network (port ?:p4)
- (port ?:p3)
- (port ?:p2)
- (port ?:p1)))
- :DIRECTIONS :backward
- :INTERNED-IN (:network-rules)
- :INFO nil
- :COMMENTS "Defines simple series networks.")
-
- (rule:rule SERIES-RESISTANCE
- :ANTECEDENT
- (:seq
- (series-network (port ?:p1)
- (port ?:p2)
- (port ?:p3)
- (port ?:p4))
- (resistance (port ?:p1) (port ?:p2) (?:scale ?:r1))
- (resistance (port ?:p3) (port ?:p4) (?:scale ?:r2))
- (:bind ?:r (+ ?:r1 ?:r2)))
- :CONSEQUENT (resistance (port ?:p1) (port ?:p4) (?:scale ?:r))
- :DIRECTIONS :backward
- :INTERNED-IN (:network-rules)
- :INFO nil
- :COMMENTS "Finds the resistance of a series network by adding the resistance of the componenets.")
-
- (rule:rule VOLTAGE-ACROSS-DEVICE-PORTS
- :ANTECEDENT
- (:and (ports (?:dev ?:d) (port ?:p1) (port ?:p2))
- (voltage (?:dev ?:d) (?:scale ?:v)))
- :CONSEQUENT
- (:and (voltage (port ?:p1) (port ?:p2) (?:scale ?:v))
- (voltage (port ?:p2) (port ?:p1) (?:scale (:lisp (- ?:v)))))
- :DIRECTIONS :forward
- :INTERNED-IN (:network-rules)
- :INFO ((:domain . :networks))
- :COMMENTS "Assigns the electromotive voltage to the ports of a device.")
-
- (rule:rule VOLTAGE-PROPAGATION
- :ANTECEDENT
- (:and (wire (port ?:p1) (port ?:p3))
- (wire (port ?:p2) (port ?:p4))
- (voltage (port ?:p1) (port ?:p2) (?:scale ?:v)))
- :CONSEQUENT (voltage (port ?:p3) (port ?:p4) (?:scale ?:v))
- :DIRECTIONS :backward
- :INTERNED-IN (:network-rules)
- :INFO ((:domain . :networks))
- :COMMENTS
- "Propagate voltage to directly connected terminals. Voltage conjunct
- of antecedent must be last to prevent circularity.")
-
- (rule:rule VOLTAGE-PROPAGATION-FLIP-SIGN
- :ANTECEDENT
- (:seq (voltage-across (port ?:p1) (port ?:p2) (volts ?:v))
- (connected (port ?:p1) (port ?:p3))
- (connected (port ?:p2) (port ?:p4))
- (:bind ?:neg-v (- ?:v)))
- :CONSEQUENT (voltage-across (port ?:p4) (port ?:p3) (volts ?:neg-v))
- :DIRECTIONS :backward
- :INTERNED-IN (:network-rules)
- :INFO ((:domain . :networks))
- :COMMENTS "Propagate voltage to directly connected terminals, where voltage must be negated.")
-
- (dnet:make-dnet :network-rules)
- (rule:add-all-rules)
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;; CIRCUIT REASONING - evaluate each form one at a time.
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
- ;;; Turn rule trace on if you wish. Then evaluate these ...
-
- ;;; Do forward chaining to get some simple facts.
-
- (rule:forward-chain :circuit-1 :network-rules)
-
- ;;; Simple retrieval of something got by forward chaining -- compare datum just.
-
- (rule:support '(voltage (port pb1-1) (port pb1-2) (volts ?:v))
- :circuit-1 :network-rules :include-datum-justification nil)
- (demo-graph-support-tree rule:*support-tree*)
-
- (rule:support '(voltage (port pb1-1) (port pb1-2) (volts ?:v))
- :circuit-1 :network-rules :include-datum-justification t)
- (demo-graph-support-tree rule:*support-tree*)
-
- ;;; Something that takes application of one voltage propagation rule.
-
- (rule:support '(voltage (port pl1-1) (port pl1-2) (volts ?:v))
- :circuit-1 :network-rules)
- (demo-graph-support-tree rule:*support-tree*)
-
- ;;; This requires problem solving across resistor to get voltage.
- ;;; Makes a rather complex graph.
-
- (dnet:defvariable "LUM")
- (rule:support '(brightness (light l1) (lumens ?:lum))
- :circuit-1 :network-rules :record-failure nil)
- (demo-graph-support-tree rule:*support-tree*)
-
- ;;; A harder test, this takes work to get past the resistor.
-
- (rule:support '(brightness (light l2) (lumens ?:lum))
- :circuit-1 :network-rules :record-failure nil)
- (demo-graph-support-tree rule:*support-tree*)
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;; MORE TESTS - Taken from Eriksson & Johannason AAAI-85.
- ;;; This part of the demo shows some problems with this backchainer.
- ;;; It does redundant reasoning, and needs a TMS to prevent this.
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;;; The database of facts. Evaluate as a group.
-
- (dnet:make-dnet ':EE-DB
- :indexpr-hook 'NIL
- :delexpr-hook 'NIL
- :info 'NIL)
-
- ;;; John
- ;;; / \
- ;;; f f
- ;;; / \
- ;;; Ada Per
- ;;; / \
- ;;; f f
- ;;; / \
- ;;; Eva Pia
- ;;; | |
- ;;; m m
- ;;; | |
- ;;; Tom Tim
-
- (dnet:indexpr '(FATHER ADA JOHN) ':EE-DB '(#S(RULE:JUSTIFICATION RULE::WARRANT :ASSERTED RULE::GROUNDS :DAN-SAYS-SO TIME 2808797026)))
- (dnet:indexpr '(FATHER PER JOHN) ':EE-DB '(#S(RULE:JUSTIFICATION RULE::WARRANT :ASSERTED RULE::GROUNDS :DAN-SAYS-SO TIME 2808797032)))
- (dnet:indexpr '(FATHER PIA PER) ':EE-DB '(#S(RULE:JUSTIFICATION RULE::WARRANT :ASSERTED RULE::GROUNDS :DAN-SAYS-SO TIME 2808797040)))
- (dnet:indexpr '(FATHER EVA PER) ':EE-DB '(#S(RULE:JUSTIFICATION RULE::WARRANT :ASSERTED RULE::GROUNDS :DAN-SAYS-SO TIME 2808797048)))
- (dnet:indexpr '(MOTHER TOM EVA) ':EE-DB '(#S(RULE:JUSTIFICATION RULE::WARRANT :ASSERTED RULE::GROUNDS :DAN-SAYS-SO TIME 2808797062)))
- (dnet:indexpr '(MOTHER TIM PIA) ':EE-DB '(#S(RULE:JUSTIFICATION RULE::WARRANT :ASSERTED RULE::GROUNDS :DAN-SAYS-SO TIME 2808797070)))
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;; Rules
-
- (dnet:make-dnet :ee-rb)
-
- (rule:defvariables "X" "Y" "Z")
-
- (rule:rule RULE-1
- :ANTECEDENT (father ?:x ?:y)
- :CONSEQUENT (parent ?:x ?:y)
- :DIRECTIONS :both
- :INTERNED-IN (:ee-rb)
- :INFO nil
- :COMMENTS "Your father is your parent.")
- (rule:add-rule 'rule-1 :ee-rb)
-
- (rule:rule RULE-2
- :ANTECEDENT (mother ?:x ?:y)
- :CONSEQUENT (parent ?:x ?:y)
- :DIRECTIONS :both
- :INTERNED-IN (:ee-rb)
- :INFO nil
- :COMMENTS "Your mother is your parent.")
- (rule:add-rule 'rule-2 :ee-rb)
-
- (rule:rule RULE-3
- :ANTECEDENT (parent ?:x ?:y)
- :CONSEQUENT (ancestor ?:x ?:y)
- :DIRECTIONS :both
- :INTERNED-IN (:ee-rb)
- :INFO nil
- :COMMENTS "Your parent is an ancestor.")
- (rule:add-rule 'rule-3 :ee-rb)
-
- (rule:rule RULE-4
- :ANTECEDENT (:and (parent ?:x ?:z) (ancestor ?:z ?:y))
- :CONSEQUENT (ancestor ?:x ?:y)
- :DIRECTIONS :both
- :INTERNED-IN (:ee-rb)
- :INFO nil
- :COMMENTS "A parent of an ancestor is also an ancestor.")
- (rule:add-rule 'rule-4 :ee-rb)
-
- (rule:rule RULE-5
- :ANTECEDENT (ancestor ?:x ?:y)
- :CONSEQUENT (related ?:x ?:y)
- :DIRECTIONS :both
- :INTERNED-IN (:ee-rb)
- :INFO nil
- :COMMENTS "You are related to your ancestor.")
- (rule:add-rule 'rule-5 :ee-rb)
-
- (rule:rule RULE-6
- :ANTECEDENT (:and (ancestor ?:x ?:z) (ancestor ?:y ?:z))
- :CONSEQUENT (related ?:x ?:y)
- :DIRECTIONS :both
- :INTERNED-IN (:ee-rb)
- :INFO nil
- :COMMENTS "You are related to anyone with a common ancestor.")
- (rule:add-rule 'rule-6 :ee-rb)
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;; Tests.
-
- ;;; (trace rule:retrieve rule::retrieve-bindings rule::retrieve-backchain
- ;;; rule::retrieve-and rule::retrieve-or)
-
- ;;; Simple.
-
- (rule:retrieve '(father ?:x john) :ee-db :ee-rb)
-
- (rule:support '(father ?:x john) :ee-db :ee-rb)
- (demo-graph-support-tree rule:*support-tree*)
-
- (rule:retrieve '(ancestor ?:x per) :ee-db :ee-rb)
-
- (rule:support '(ancestor ?:x per) :ee-db :ee-rb)
- (demo-graph-support-tree rule:*support-tree*)
-
- ;;; This can take a while.
-
- (rule:support '(related ada ?:x) :ee-db :ee-rb)
- (demo-graph-support-tree rule:*support-tree*)
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;; EOF
-